<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实时工资计算器</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            font-family: 'Arial', sans-serif;
        }
        .calculator-container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0,0,0,0.1);
            padding: 40px;
            margin-top: 50px;
        }
        .earnings-display {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 15px;
            padding: 30px;
            margin: 20px 0;
        }
        .earnings-amount {
            font-size: 2.5rem;
            font-weight: bold;
            margin: 10px 0;
        }
        .update-indicator {
            font-size: 0.9rem;
            opacity: 0.7;
        }
        .form-label {
            font-weight: 600;
            color: #495057;
        }
        .btn-calculate {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            padding: 12px 30px;
            font-weight: 600;
        }
        .btn-calculate:hover {
            background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8 col-lg-6">
                <div class="calculator-container">
                    <h1 class="text-center mb-4">
                        <i class="fas fa-calculator text-primary me-2"></i>
                        实时工资计算器
                    </h1>
                    
                    <form id="salaryForm">
                        <div class="mb-3">
                            <label for="hourlyRate" class="form-label">
                                <i class="fas fa-coins me-1"></i>时薪 (元/小时)
                            </label>
                            <input type="number" class="form-control" id="hourlyRate" 
                                   placeholder="请输入时薪" min="0" step="0.01" value="50" required>
                        </div>
                        
                        <div class="mb-3">
                            <label for="dailyHours" class="form-label">
                                <i class="fas fa-clock me-1"></i>每日工作小时数
                            </label>
                            <input type="number" class="form-control" id="dailyHours" 
                                   placeholder="请输入每日工作小时数" min="0" max="24" step="0.5" value="8" required>
                        </div>
                        
                        <div class="mb-3">
                            <label for="workDays" class="form-label">
                                <i class="fas fa-calendar me-1"></i>每月工作日
                            </label>
                            <input type="number" class="form-control" id="workDays" 
                                   placeholder="请输入每月工作日" min="1" max="31" value="22" required>
                        </div>
                        
                        <button type="submit" class="btn btn-primary btn-calculate w-100">
                            <i class="fas fa-sync-alt me-1"></i>开始计算
                        </button>
                    </form>
                    
                    <div class="earnings-display">
                        <h5 class="text-center mb-3">
                            <i class="fas fa-chart-line me-1"></i>实时收入
                        </h5>
                        
                        <div class="text-center">
                            <div class="mb-4">
                                <h6>今日已赚金额</h6>
                                <div class="earnings-amount" id="dailyEarnings">¥0.00</div>
                                <small class="update-indicator">每秒更新</small>
                            </div>
                            
                            <div>
                                <h6>本月已赚金额</h6>
                                <div class="earnings-amount" id="monthlyEarnings">¥0.00</div>
                                <small class="update-indicator">每秒更新</small>
                            </div>
                        </div>
                    </div>
                    
                    <div class="text-center text-muted mt-3">
                        <small>
                            <i class="fas fa-info-circle me-1"></i>
                            基于当前时间实时计算，刷新页面将重置计时
                        </small>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
    <script>
        class SalaryCalculator {
            constructor() {
                this.hourlyRate = 50;
                this.dailyHours = 8;
                this.workDays = 22;
                this.startTime = new Date();
                this.updateInterval = null;
                
                this.init();
            }
            
            init() {
                this.loadFromForm();
                this.bindEvents();
                this.startCalculation();
            }
            
            bindEvents() {
                const form = document.getElementById('salaryForm');
                form.addEventListener('submit', (e) => {
                    e.preventDefault();
                    this.loadFromForm();
                    this.startCalculation();
                });
                
                // 实时更新输入值
                ['hourlyRate', 'dailyHours', 'workDays'].forEach(id => {
                    document.getElementById(id).addEventListener('input', () => {
                        this.loadFromForm();
                        this.updateDisplay();
                    });
                });
            }
            
            loadFromForm() {
                this.hourlyRate = parseFloat(document.getElementById('hourlyRate').value) || 0;
                this.dailyHours = parseFloat(document.getElementById('dailyHours').value) || 0;
                this.workDays = parseInt(document.getElementById('workDays').value) || 0;
            }
            
            startCalculation() {
                if (this.updateInterval) {
                    clearInterval(this.updateInterval);
                }
                
                this.updateInterval = setInterval(() => {
                    this.updateDisplay();
                }, 1000);
                
                this.updateDisplay();
            }
            
            calculateDailyEarnings() {
                const now = new Date();
                const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 9, 0, 0);
                const todayEnd = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 18, 0, 0);
                
                if (now < todayStart) return 0;
                if (now > todayEnd) return this.hourlyRate * this.dailyHours;
                
                const workingSeconds = (now - todayStart) / 1000;
                const workingHours = Math.min(workingSeconds / 3600, this.dailyHours);
                
                return workingHours * this.hourlyRate;
            }
            
            calculateMonthlyEarnings() {
                const now = new Date();
                const monthStart = new Date(now.getFullYear(), now.getMonth(), 1);
                
                const totalDays = Math.floor((now - monthStart) / (1000 * 60 * 60 * 24)) + 1;
                const workingDays = Math.min(totalDays, this.workDays);
                
                const dailyEarnings = this.hourlyRate * this.dailyHours;
                const fullDaysEarnings = workingDays * dailyEarnings;
                
                // 计算当前工作日的收入
                const currentDayEarnings = this.calculateDailyEarnings();
                
                return fullDaysEarnings - dailyEarnings + currentDayEarnings;
            }
            
            updateDisplay() {
                const dailyEarnings = this.calculateDailyEarnings();
                const monthlyEarnings = this.calculateMonthlyEarnings();
                
                document.getElementById('dailyEarnings').textContent = 
                    `¥${dailyEarnings.toFixed(2)}`;
                document.getElementById('monthlyEarnings').textContent = 
                    `¥${monthlyEarnings.toFixed(2)}`;
            }
        }
        
        // 初始化计算器
        document.addEventListener('DOMContentLoaded', () => {
            new SalaryCalculator();
        });
    </script>
</body>
</html>
